package middleware

import (
	"github.com/gin-gonic/gin"
	"runtime/debug"

	"go-caipu/pkg/infra/log"
)

func ZapRecovery(logger log.Logger) gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				logger.Error("Recovered from panic", "error", err, "stack", string(debug.Stack()))
				c.AbortWithStatusJSON(500, gin.H{"error": "Internal Server Error"})
			}
		}()
		c.Next()
	}
}
